The HttpAgent Component
What Is the HttpAgent Component?
Every time your application uses the CFHTTP tag, it spends a sizable amount of time waiting for a response from the HTTP request. During that time, your application sits idle. This may be all right if you have only a single request to make but imagine if you have dozens or hundreds to make. Wouldn't it be great if you could put that time to better use? HttpAgent does just that. Instead of executing a single request and having it block (wait for response), you can execute as many simultaneous connections as you wish. While one request waits for data, other requests can perform real work, such as downloading or processing a request. This simultaneous approach has huge performance benefits. HttpAgent gives you the convenience of CFHTTP as well as the sophistication of performing simultaneous requests.
Note: This product includes software developed by the Apache Group for use in the Apache HTTP server project. Specifically, HttpAgent uses a tool called HttpClient available through the Apache Jakarta project.
Installing the HttpAgent Component
Before installing HttpAgent, shut down your ColdFusion server. Then copy httpagent.jar and httpclient.jar to wwwroot/WEB-INF/lib, where wwwroot appears under the CFusionMX installation folder.
After you copy the custom tags in the tags folder to wwwroot/cf_tags/httpagent/, copy the examples folder to a location under wwwroot.
Finally, start your ColdFusion server, browse to the examples folder you installed under webroot, and experiment with the included samples.
Using the HttpAgent Component
Consider the following example:
1 <cfimport taglib="/cf_tags/httpagent" prefix="h"/> 2 <h:agent threadCount = "5" 3 timeout = "4" 4 returnVariable = "results"> 5 <cfloop index = "u" 6 list = #form.urls# 7 delimiters = "#chr(10)##chr(13)#"> 8 <h:request key = "url-#i#" 9 url = "#u#" 10 username = "#form.username#" 11 password = "#form.password#" 12 method = "#form.method#" 13 userAgent = "#form.userAgent#"/> 14 </cfloop> 15 </h:agent>
Line 1 imports the HttpAgent tags and sets up the prefix for those tags to be the letter h.
Lines 2–4 create a new HttpAgent, setting it to run five requests (threads) simultaneously with a timeout of four seconds. That means that requests that take more than four seconds to occur fail automatically, ensuring that your application never hangs indefinitely on a request. Finally, the results of the HttpAgent are stored in the results variable.
Lines 5–7 loop through all the lines in the variable form.urls. In this example, form.url is expected to contain a list of URLs separated by a new line.
Lines 8–13 register a request with the agent object. This does not execute the request; it merely queues it. The attributes of the request are much like the CFHTTP tag, in that they fully describe a web request. One important attribute that you will not find in CFHTTP is the key attribute. This string allows you to retrieve the result of this particular request once it has been processed.
Line 15 executes the queued up requests. HttpAgent spawns the number of parallel processes described by the threadCount attribute. Keep in mind that no work is actually performed until the HttpAgent tag is encountered. Once the tag completes execution, the results of the execution are returned in the variable defined by the returnVariable attribute. This variable is a struct whose keys are associated with each request and whose values are com.macromedia.net.HttpAgentResponse objects.
Below is an example that uses the results of the HttpAgent tag. Assume that the results variable contains your processed requests.
16 <cfloop index = "k" 17 list = #structKeyList(results)#> 18 <cfoutput> 19 <tr> 20 <td>[#results[k].getResponseCode()#] 21 #results[k].getResponseMessage()#</td> 22 <td> 23 #results[k].getRequest().getUrl()# 24 </td> 25 </tr> 26 </cfoutput> 27 </cfloop>
Lines 16–17 iterate through all the keys in the results.
Line 20 extracts a particular result and calls the getResponseCode method on it. This returns the numeric code associated with the success or failure of this HTTP request (for example, 404 means "page not found").
Line 21 performs a similar operation but invokes the getResponseMessage method, which returns a textual description of the status.
Line 23 extracts the original HttpAgentRequest object from a particular response and invokes the getUrl method on it. This demonstrates the ability to access the request parameters used to create a particular response. In this case, you access the URL that actually created this particular response. See the included examples and the javadoc for more details on the methods available, as well as attributes to the agent and request tags.
Implementing the HttpAgent Component
The HttpAgent custom tags are written on top of a Java API that glues two important resources together. First, HttpAgent makes use of the Thread object provided by Java, which is what allows you to spawn simultaneous processes. Second, HttpAgent makes use of the HTTP client package provided by the Apache group. This package is a Java API that provides a much more advanced interface than standard Java objects for dealing with URLs and HTTP. For example, the ability to time out a request is not available in the standard Java network package; however, it is provided in the HTTP client. By gluing these two resources together, HttpAgent provides you with a simple interface to what would otherwise be the complex task of performing simultaneous requests.